/*  This is a very basic example for a steganographic editor, which lacks
    several important features like ommitable words or telling the user the
    actual number of words perse entered. It doesn't even take the nibble as
    user input but has them hard-coded, since this is only a test project.

    TODO:

        * Generate nibbles based on a file (with a reasonable offset like 6)
        ✓ Show the user the actual number of words per sentence if they are
          not as required by the editor.
        ✓ Output the entered text to a text file that is specified as argument
        * Allow ommitable words and certain other useful editing features.

    Long-term TODO:

        * Also make a ncurses-based version that shows the number of words in
          real-time to the user.
        * (If reasonably possible) Auto-completion?
        * Use a file of words that are almost always ommitable as a reference
          for probably ommitable words. (requires a formal definition of an
          ommitable word, first)                                             */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "string_tools.h"

#define BUFFER_SIZE 1024

int main(int argc, char *argv[])
{
	char buffer[BUFFER_SIZE];
	char **out;
	uint8_t nibbles[] = {13, 11, 18, 7, 9};
	uint8_t count = 0;
	unsigned int i = 0;

    FILE *f;

    if (argc == 2)
    {
        f = fopen(argv[1], "w");
        if (f == NULL)
        {
            printf("Could not create/open file '%s'. Check write permission.\n\n", argv[1]);
            exit(EXIT_FAILURE);
        }
    }
    else if (argc == 1)
    {
        printf("No filename specified. Assuming 'stegedit_text'. Make sure to rename this\nfile later!\n\n");

        f = fopen("stegedit_text", "w");
        if (f == NULL)
        {
            printf("Could not create/open file '%s'. Check write permission.\n\n", "stegedit_text");
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        printf("Usage: %s (filename)\n\n", argv[0]);
        exit(EXIT_SUCCESS);
    }

	out = malloc(sizeof(char*) * sizeof(nibbles));

	printf(
	"\tSDTP - Steganographic Editor   Version 0.02\n\n"
	"Clear terminal after usage and destroy any physical\n"
	"prints if they have been creating during usage.\n\n"
	"Abort with CTRL+C\n\n");

	for (i = 0; i < sizeof(nibbles); i++)
	{
		while(count != nibbles[i])
		{
			printf("%-2i (%i)%c", nibbles[i], count, (count > 9) ? 0 : ' ');
			printf(": ");
			fgets(buffer, BUFFER_SIZE, stdin);
			strtok(buffer, "\n");

			count = chrn(buffer, ' ') + 1;
		}

		out[i] = malloc(strlen(buffer) + 1);
		strcpy(out[i], buffer);

		count = 0;
	}

    for (i = 0; i < sizeof(nibbles); i++)
        fprintf(f, "%s ", out[i]);

    fprintf(f, "\n");

/*	for (i = 0; i < sizeof(nibbles); i++)
		printf("%s ", out[i]);

	printf("\n"); */

	for (i = 0; i < sizeof(nibbles); i++)
		free(out[i]);

	free(out);
    fclose(f);

	return 0;
}